package club.kingyin.kyblogprouser.service.impl;

import club.kingyin.commons.constant.UserConstant;
import club.kingyin.commons.entity.user.Friend;
import club.kingyin.commons.enums.BlogEnum;
import club.kingyin.kyblogprouser.core.Distributed;
import club.kingyin.kyblogprouser.utils.RSA;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import club.kingyin.kyblogprouser.dao.UserDao;
import club.kingyin.commons.entity.user.User;
import club.kingyin.kyblogprouser.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2022-01-20 15:04:02
 */
@Transactional
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Resource
    private UserDao userDao;

    @Autowired
    private Distributed distributed;

    @Value("${easytoken.private-key}")
    private String privateKey;

    public Page<User> selectPage(Page<User> page, User user) {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        if (user.getNickname() != null)
            queryWrapper.like(User::getNickname,user.getNickname());
        if (user.getUsername() != null)
            queryWrapper .like(User::getUsername,user.getUsername());
        if (user.getRole() != null)
            queryWrapper.eq(User::getRole,user.getRole());
        if (user.getId() != null)
            queryWrapper.eq(User::getId,user.getId());
        if (user.getSex() != null)
            queryWrapper.eq(User::getSex,user.getSex());
        if (user.getStatus() != null)
            queryWrapper.eq(User::getStatus,user.getStatus());
        queryWrapper.select(User::getId, User::getAvater, User::getNickname, User::getRole, User::getUsername, User::getSex, User::getSlogan,User::getStatus,User::getTitled);
        return userDao.selectPage(page, queryWrapper);
    }

    public User selectById(Serializable id) {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getId,id).select(User::getId, User::getAvater, User::getNickname, User::getRole, User::getUsername, User::getSex, User::getSlogan);
        return userDao.selectOne(queryWrapper);
    }
    public User selectById(int id, LambdaQueryWrapper<User> wrapper) {
        wrapper.eq(User::getId,id);
        return userDao.selectOne(wrapper);
    }

    public List<User> selectUserNickname() {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(User::getId,User::getNickname);
        return userDao.selectList(queryWrapper);
    }


    public User selectByUsername(String mail) {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getUsername,mail).select(User::getId, User::getUsername, User::getStatus);
        return userDao.selectOne(queryWrapper);
    }

    public User equalsAndGet(User user) {
        try {
            System.out.println(user.getPassword());
            LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(User::getUsername, user.getUsername())
                    .eq(User::getPassword,RSA.decodeToString(user.getPassword(),privateKey))
                    .ne(User::getStatus, BlogEnum.MAIL)
                    .select(User::getId, User::getAvater, User::getNickname, User::getRole, User::getUsername, User::getSex, User::getSlogan,User::getTitled);
            return userDao.selectOne(queryWrapper);
        } catch (Exception e) {
            return null;
        }

    }

    public User insert(User user) {
        user.setId(distributed.nextId(UserConstant.USER_ID));
        user.setRole(BlogEnum.USER);
        user.setStatus(BlogEnum.MAIL);
        user.setAvater(UserConstant.USER_AVATER);
        user.setSlogan(UserConstant.USER_SLOGAN);
        user.setSex(BlogEnum.MAN);
        if (user.getNickname() == null) {
            user.setNickname("K新博主");
        }
        if (user.getPassword() != null) {
            user.setPassword(RSA.decodeToString(user.getPassword(),privateKey));
        }

        return userDao.insert(user)>0?user:null;
    }

    public boolean passByUsername(User user) {
        LambdaUpdateWrapper<User> updateWrapper = Wrappers.lambdaUpdate();

        updateWrapper.set(User::getStatus,BlogEnum.NORMAL).set(User::getPassword, RSA.decodeToString(user.getPassword(),privateKey)).eq(User::getUsername,user.getUsername());
        return userDao.update(null, updateWrapper) > 0;
    }

    public boolean updatePassword(User user) {
        LambdaUpdateWrapper<User> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.set(User::getPassword,RSA.decodeToString(user.getPassword(),privateKey)).eq(User::getUsername,user.getUsername());
        return userDao.update(null, updateWrapper) > 0;
    }

    public Page<User> getFriendsByUserIdAndStatus(Page<User> page,int userid, BlogEnum status) {
        QueryWrapper<Friend> queryWrapper = Wrappers.query();
        queryWrapper.eq("friend.id",userid);
        if (status == BlogEnum.CONCERNED) {
            queryWrapper.in("friend.status",BlogEnum.CLOSELY.getValue(), BlogEnum.CONCERNED.getValue());
        } else if (status == BlogEnum.BE_CONCERNED) {
            queryWrapper.eq("friend.status", BlogEnum.BE_CONCERNED);
        }
        return userDao.getFriends(page, queryWrapper);
    }

}

